package com.tanlu.tcc.domain;

import com.tanlu.tcc.Terminator;
import com.tanlu.tcc.util.TransactionXid;

import java.io.Serializable;

public class Participant implements Serializable {

    private String serviceName;

    private TransactionXid transactionId;

    private InvocationContext confirmInvocationContext;

    private InvocationContext cancelInvocationContext;

    public Participant(String serviceName, InvocationContext confirmInvocationContext,
                       InvocationContext cancelInvocationContext) {
        this.serviceName = serviceName;
        this.confirmInvocationContext = confirmInvocationContext;
        this.cancelInvocationContext = cancelInvocationContext;
    }

    public TransactionXid getTransactionId() {
        return transactionId;
    }

    public void setTransactionId(TransactionXid transactionId) {
        this.transactionId = transactionId;
    }

    public String getServiceName() {
        return serviceName;
    }

    public void commit() throws Exception {
        Terminator terminator = new Terminator();
        terminator.invoke(confirmInvocationContext);
    }

    public void rollback() throws Exception {
        Terminator terminator = new Terminator();
        terminator.invoke(cancelInvocationContext);
    }

}
